<?php

namespace App\Http\Controllers;

use App\Models\Admin;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use QrCode;

class InfoController extends AdminController
{
    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * 我的资料页
     */
    public function index(Request $request){
        $info= $request->user();
//       $qrcode = QrCode::format('png');
//        $qrcode=$qrcode->generate(111);
//        dd($qrcode);
        return view("info.index",["user"=>$info]);
    }

    public function  update(Request $request){

        if($request->isMethod("post")){
            $info= $request->user();
            $user = Admin::find($info->id);

            $name =trim($request->name);
            if(!empty($name)){
                $user->name=$name;
            }
            $phone = $request->phone;
            if(!empty($phone)){
                $user->phone=$phone;
            }
            $card = $request->card;
            if(!empty($card)){
                $user->card=$card;
            }
            $password = $request->password;
            $password2 = $request->password2;
            if(!empty($password) || !empty($password2) ){
                if($password==$password2){
                    $user->password=bcrypt($password);
                }else{
                    return back()
                        ->withErrors(array("两次输入的密码不相同"))
                        ->withInput();
                }

            }
           if($user->save()){
               if(!empty($password)){
                   return redirect("/logout");
               }else{
                   $this->setlog(json_encode($user),'代理修改个人资料');
                   return redirect("/info")->with("messages", ['0' => "修改成功"]);;
               }

           }else{
               return back()
               ->withErrors(array("修改失败"))
               ->withInput();

           }
        }else{
            return back()
                ->withErrors(array("非法提交"))
                ->withInput();
        }

    }






}
